//
//  ZYCAttendanceExportRecordsViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/26.
//

#import "ZYCAttendanceExportRecordsViewController.h"
#import "ZYCAttendanceExportRecordsCell.h"
#import "ZYCNoDataView.h"
#import "SESSFileLookController.h"

@interface ZYCAttendanceExportRecordsViewController ()<UITableViewDataSource,UITableViewDelegate,ZYCAttendanceExportRecordsCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) ZYCNoDataView *noDataView;

@end

@implementation ZYCAttendanceExportRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"导出记录";
    [self.view addSubview:self.tableView];
    [self reloadTableDatas];

}

- (NSString *)createAttendanceSummaryPathFilePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSString *attendanceSummaryPath = [cachePath stringByAppendingPathComponent:@"attendanceSummary"];
    // 创建文件夹(考勤汇总)
    [fileManager createDirectoryAtPath:attendanceSummaryPath withIntermediateDirectories:YES attributes:nil error:nil];
    return attendanceSummaryPath;
}

- (void)reloadTableDatas{
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:[self createAttendanceSummaryPathFilePath]];

    self.dataSource = enumerator.allObjects.mutableCopy;
    if (self.dataSource.count == 0) {
        self.tableView.hidden = YES;
        self.noDataView.hidden = NO;
    }else{
        self.tableView.hidden = NO;
        self.noDataView.hidden = YES;
    }
    
    [self.tableView reloadData];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCAttendanceExportRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCAttendanceExportRecordsCell class]) forIndexPath:indexPath];
    cell.filePath = self.dataSource[indexPath.section];
    cell.delegate = self;
    return cell;
}


-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *filePath = self.dataSource[indexPath.row];
    [self.dataSource removeObject:filePath];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:[self createAttendanceSummaryPathFilePath]];
    [enumerator.allObjects enumerateObjectsUsingBlock:^(NSString *subPath, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSLog(@"%@~~~~%zd",subPath,idx);
        /* 错误处理 */
        NSError *error = nil;
        if ([filePath isEqualToString:subPath]) {
            NSString *path = [[self createAttendanceSummaryPathFilePath] stringByAppendingPathComponent:subPath];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                if (error) {
//                    NSLog(@"文件删除失败");
                }else {
//                    NSLog(@"文件删除成功");
                }
            }
        }
    }];
    
    [self.tableView reloadData];
    
}

#pragma mark - ZYCAttendanceExportRecordsCellDelegate
- (void)attendanceExportRecordsCellDidPreview:(NSString *)filePath{
    NSString *path = [[self createAttendanceSummaryPathFilePath] stringByAppendingPathComponent:filePath];
    SESSFileLookController *vc = [[SESSFileLookController alloc] initWithFilePath:[NSURL fileURLWithPath:path]];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}
#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCAttendanceExportRecordsCell class] forCellReuseIdentifier:NSStringFromClass([ZYCAttendanceExportRecordsCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}


- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
